#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：17_多进程拷贝文件.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/6/7/007 21:02 
"""
"""
流程
1、创建一个文件夹
2、获取old文件中所有文件名字
3、使用多进程的方式copy，源文件夹中的所有复制到新的文件夹
"""
import os
from multiprocessing import Pool,Manager


def copyFileTask(name, oldFolderName, newFolderName,queue):
    """
    完成拷贝一个文件的功能
    :return:
    """
    with open(oldFolderName+"/"+name,"r", encoding="utf-8") as fr:
        r = fr.read()
    with open(newFolderName+"/"+name,"a+",encoding="utf-8") as fw:
        w = fw.write(r)
    queue.put(name)


def main():
    oldFolderName = input("请输入文件夹的名字：")
    newFolderName = oldFolderName + "附件"
    print("新的文件夹名称：" + newFolderName)
    os.mkdir(newFolderName)
    path_list = os.listdir(oldFolderName)
    # print(path_list)
    pool = Pool(5)
    queue = Manager().Queue()
    for name in path_list:
        pool.apply_async(copyFileTask, args=(name, oldFolderName, newFolderName,queue))

    num = 0
    allNum = len(path_list)
    while num < allNum:
        queue.get()
        num += 1
        copyRate = num/allNum
        print("\rcopy的进度是：%.2f%%"%(copyRate*100),end="")
    print("已完成拷贝。\n")


if __name__ == '__main__':
    main()
